package com.zdlee.netty.server15.codec;

import com.zdlee.netty.server15.command.PacketCodeC;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * @author zdlee
 * @description: ${DESCRIPTION}
 * @create 2018-11-30 14:25
 */
public class Spliter extends LengthFieldBasedFrameDecoder {
    private static final int LENGTH_FIELD_OFFSET=7;
    private static final int LENGTH_FIELD_LENGTH=4;

    public Spliter(){
        super(Integer.MAX_VALUE, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH);
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        if(in.getInt(in.readerIndex())!= PacketCodeC.MAGIC_NUMBER)
        {
            ctx.channel().close();
            return null;
        }
        return super.decode(ctx, in);
    }
}
